package com.ruoyi.controller.team;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.team.domain.vo.MgTeam;
import com.ruoyi.team.domain.vo.TeamOrder;
import com.ruoyi.team.domain.vo.TeamUser;
import com.ruoyi.team.service.ITeamInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 团队Controller
 *
 * @author ruoyi
 * @date 2024-01-08
 */
@RestController
@RequestMapping("/team/information")
public class TeamInformationController extends BaseController {
    @Autowired
    private ITeamInformationService teamInformationService;

    /**
     * 查询团队列表
     */
    @PreAuthorize("@ss.hasPermi('team:information:list')")
    @GetMapping("/list")
    public TableDataInfo list(MgTeam mgTeam) {
        startPage();
        List<MgTeam> list = teamInformationService.selectTeamList(mgTeam);
        return getDataTable(list);
    }

    /**
     * 导出团队列表
     */
    @PreAuthorize("@ss.hasPermi('team:information:export')")
    @Log(title = "团队", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MgTeam mgTeam)
    {
        List<MgTeam> list = teamInformationService.selectTeamList(mgTeam);
        ExcelUtil<MgTeam> util = new ExcelUtil<MgTeam>(MgTeam.class);
        util.exportExcel(response, list, "团队数据");
    }
//
    /**
     * 获取团队详细信息
     */
    @PreAuthorize("@ss.hasPermi('team:information:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(teamInformationService.selectTeamById(id));
    }
//
//    /**
//     * 新增团队
//     */
//    @PreAuthorize("@ss.hasPermi('team:information:add')")
//    @Log(title = "团队", businessType = BusinessType.INSERT)
//    @PostMapping
//    public AjaxResult add(@RequestBody MgTeam mgTeam)
//    {
//        return toAjax(mgTeamService.insertMgTeam(mgTeam));
//    }
//
    /**
     * 修改团队
     */
    @PreAuthorize("@ss.hasPermi('team:information:edit')")
    @Log(title = "团队", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MgTeam mgTeam)
    {
        MgTeam data =new MgTeam();
        data.setId(mgTeam.getId());
        data.setRemark(mgTeam.getRemark());
        data.setDiscount(mgTeam.getDiscount());
        teamInformationService.updateMgTeam(data);
        return toAjax(1);
    }

    @GetMapping("/userList")
    public TableDataInfo userList(MgTeam mgTeam) {
        List<TeamUser> list = teamInformationService.getUserListByTeamId(mgTeam.getId());
        return getDataTable(list);
    }

    @GetMapping("/orderList")
    public TableDataInfo orderList(MgTeam mgTeam) {
        List<TeamOrder> list = teamInformationService.getOrderListByTeamId(mgTeam.getId());
        return getDataTable(list);
    }
}
